package com.hankol.winecellar

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.activity.BaseActivity
import com.baselibrary.extension.checkNotNull
import com.baselibrary.extension.checkValue
import com.baselibrary.extension.load
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.*
import com.baselibrary.adapter.OrderBottomItemAdapter
import com.baselibrary.bean.OrderBottomItemBean
import com.hankol.winecellar.bean.AfterSalesInfoBean
import com.hankol.winecellar.databinding.ActivityGoodsAfterSalesDetailsBinding
import com.baselibrary.utils.AmountUtil
import com.baselibrary.utils.OptionUtils
import com.hankol.winecellar.viewmodel.WineCellarViewModel

/**新零售-二期-经营者 售后详情
 * @author 关忆北、
 * @time 2022/8/20 8:40 下午
 */
@Route(path = ActivityRouter.WINE_CELLAR.AFTER_SALE_DETAIL)
class GoodsAfterSalesDetailsActivity : BaseActivity() {
    private var afterSalesId: Long = 0
    private var afterSalesInfoBean: AfterSalesInfoBean? = null
    private lateinit var viewModel: WineCellarViewModel
    private lateinit var dataBinding: ActivityGoodsAfterSalesDetailsBinding
    private val mAdapter1 by lazy { OrderBottomItemAdapter(emptyList()) }
    private val mAdapter2 by lazy { OrderBottomItemAdapter(emptyList()) }
    val mAdapterList1 = mutableListOf<OrderBottomItemBean>()
    val mAdapterList2 = mutableListOf<OrderBottomItemBean>()

    companion object {
        const val AFTER_SALES_ID = "afterSalesId"
    }

    override fun getLayoutId(): Int {
        initImmersion(true)
        return R.layout.activity_goods_after_sales_details
    }

    override fun init() {
        setStatusBarHeight(R.color.color_F9F9F9)
        StatusBarUtil.setStatusBarDarkTheme(this, true)
        dataBinding = getDataBinding<ActivityGoodsAfterSalesDetailsBinding>()
        viewModel = getActivityViewModelProvider(this).get(WineCellarViewModel::class.java)
        initData()
        loadData()
        initListener()
        initObserve()
    }

    fun initData() {
        if (ARouterUtils.parseParams(intent.extras!!, AFTER_SALES_ID)[0].checkNotNull()) {
            afterSalesId = ARouterUtils.parseParams(intent.extras!!, AFTER_SALES_ID)[0].toLong()
        }
    }

    fun loadData() {
        viewModel.reqLoadAfterSaleInfo(afterSalesId)
        dataBinding.rvAfterSales1.adapter = mAdapter1
        dataBinding.rvAfterSales2.adapter = mAdapter2
    }

    fun initListener() {
        dataBinding.backImageView.setOnClickListener { finish() }
        dataBinding.ivCustomerService.setOnClickListener {
            //客服
            if (!DoubleClickHelper.isOnDoubleClick()) {
                ARouterUtils.jump(ActivityRouter.MINE_CENTER.CUSTOMER_SERVICE, this)
            }
        }
        dataBinding.rlGoods.setOnClickListener {
            afterSalesInfoBean?.products?.let {
                //商品详情
                val map = HashMap<String, Any>()
                map[Constants.GOODS_DETAIL_ID] = it.get(0).spuId
                map[Constants.GOODS_SKU_ID] = it.get(0).skuId
                ARouterUtils.jump(ActivityRouter.NEW_RETAIL.GOODS_DETAUILS, map)
            }
        }
    }

    private fun initObserve() {
        viewModel.afterSaleInfoLD.observe(this, Observer {
            hideLoadingDialog()
            if (it != null) {
                this.afterSalesInfoBean = it
                when (it.afterSaleStatus) {
                    0 -> {
                        dataBinding.tvTopStatus.text = "处理中"
                        dataBinding.imgTopStatus.load(this, R.drawable.ic_after_sales_details)
                        dataBinding.llAfterSales1.visibility = View.VISIBLE
                        dataBinding.llAfterSales2.visibility = View.GONE
                        mAdapterList1.add(OrderBottomItemBean("售后编号", it.afterSaleCode.checkValue(), Constants.TYPE_HAVE_BUTTON))
                        mAdapterList1.add(OrderBottomItemBean("订单编号", it.productOrderCode.checkValue(), Constants.TYPE_HAVE_BUTTON))
                        mAdapterList1.add(OrderBottomItemBean("申请时间", TimeUtil.setYearMonthDay(it.requestTime), Constants.TYPE_TXT))
                    }
                    1 -> {
                        dataBinding.tvTopStatus.text = "已通过"
                        dataBinding.imgTopStatus.load(this, R.drawable.ic_status_complate)
                        dataBinding.llAfterSales1.visibility = View.VISIBLE
                        dataBinding.llAfterSales2.visibility = View.VISIBLE
                        mAdapterList1.add(OrderBottomItemBean("审核退款数量", it.auditReturnNum.toString(), Constants.TYPE_TXT))
                        mAdapterList1.add(OrderBottomItemBean("审核退款金额", "¥${AmountUtil.changeF2YKeepTwo(it.auditReturnPrice.toString())}", Constants.TYPE_TXT))
                        if (it.auditReturnScore > 0) {
                            mAdapterList1.add(OrderBottomItemBean("审核退还酒币", it.auditReturnScore.toString(), Constants.TYPE_TXT))
                        }
                        if (it.auditReturnGsScore > 0) {
                            mAdapterList1.add(OrderBottomItemBean("审核退还尊享币", it.auditReturnGsScore.toString(), Constants.TYPE_TXT))
                        }
                        mAdapterList1.add(OrderBottomItemBean("退款方式", "原支付返还", Constants.TYPE_TXT))

                        mAdapterList2.add(OrderBottomItemBean("售后编号", it.afterSaleCode.checkValue(), Constants.TYPE_HAVE_BUTTON))
                        mAdapterList2.add(OrderBottomItemBean("订单编号", it.productOrderCode.checkValue(), Constants.TYPE_HAVE_BUTTON))
                        mAdapterList2.add(OrderBottomItemBean("售后申请时间", TimeUtil.setYearMonthDay(it.requestTime), Constants.TYPE_TXT))
                        mAdapterList2.add(OrderBottomItemBean("售后同意时间", TimeUtil.setYearMonthDay(it.auditTime), Constants.TYPE_TXT))
                    }
                    2 -> {
                        dataBinding.tvTopStatus.text = "已拒绝"
                        dataBinding.imgTopStatus.load(this, R.drawable.ic_status_refuse)
                        dataBinding.llAfterSales2.visibility = View.GONE
                        dataBinding.llAfterSales1.visibility = View.VISIBLE
                        mAdapterList1.add(OrderBottomItemBean("售后编号", it.afterSaleCode.checkValue(), Constants.TYPE_HAVE_BUTTON))
                        mAdapterList1.add(OrderBottomItemBean("订单编号", it.productOrderCode.checkValue(), Constants.TYPE_HAVE_BUTTON))
                        mAdapterList1.add(OrderBottomItemBean("售后申请时间", TimeUtil.setYearMonthDay(it.requestTime), Constants.TYPE_TXT))
                        mAdapterList1.add(OrderBottomItemBean("售后拒绝时间", TimeUtil.setYearMonthDay(it.auditTime), Constants.TYPE_TXT))
                    }
                }

                mAdapter1.setNewData(mAdapterList1)
                mAdapter2.setNewData(mAdapterList2)

                dataBinding.tvAfterSalesName.text = it.products[0].spuName
                dataBinding.tvAfterSalesNorms.text = OptionUtils.getNormsStr(it.products[0].specList)
                dataBinding.tvAfterSalesQuantity.text = "申请数量：${it.products[0].num}"
                dataBinding.tvAfterSalesRefundPrice.text = "¥${AmountUtil.changeF2YKeepTwo(if (it.afterSaleStatus == 1) it.auditReturnPrice.toString() else it.afterSaleRequestPrice.toString())}"
                dataBinding.tvAfterSalesReason.text = viewModel.reasonStrList[it.afterSaleReason.minus(1)]//后台返回的是从1开始的
                dataBinding.tvAfterSalesDes.text = it.afterSaleDesc

                val nineGridPhoto = dataBinding.photoContentView
                if (it.afterSalePic.checkNotNull()) {
                    nineGridPhoto.visibility = View.VISIBLE
                    val urlList = ArrayList<String>()
                    val typeList = ArrayList<Int>()
                    for (media in it.afterSalePic) {
                        typeList.add(media.type)
                        if (media.type == 1) {
                            urlList.add(media.videoUrl)
                        } else {
                            urlList.add(media.coverUrl)
                        }
                    }
                    nineGridPhoto.setImageUrls(urlList, typeList)
                    nineGridPhoto.spacing = ScreenUtils.dpToPx(5).toFloat()
                    nineGridPhoto.setCallback { position -> //图片详情
                        val bundle = Bundle()
                        bundle.putStringArrayList(Constants.KEY_URLS, urlList)
                        bundle.putInt(Constants.KEY_INDEX, position)
                        bundle.putStringArrayList(Constants.KEY_COMMENT_CONTENT, arrayListOf())
                        bundle.putStringArrayList(Constants.KEY_COMMENT_TITLE, arrayListOf())
                        ActivityRouter.startActivity(ActivityRouter.WINE_CELLAR.IMG_VIDEO_DETAIL, bundle)
                    }
                } else {
                    nineGridPhoto.visibility = View.GONE
                }
                dataBinding.tvPicVoucher.visibility = if (it.afterSalePic.isEmpty()) View.GONE else View.VISIBLE
                GlideUtils.loadImg_101(applicationContext, it.products[0].skuCover, dataBinding.ivAfterSalesCover)
            }
        })
    }
}